<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Orders;
use Illuminate\Http\Request;

class OrdersController extends Controller
{
    // 列表
    public function index(Request $request)
    {
        $searchData = $request->post();

        $model = Orders::from('orders as o')
            ->orderBy('o.id', 'DESC')
            ->leftJoin('users', 'users.id', '=', 'o.user_id')
            ->select([
                'oc.*', 'users.nickname'
            ]);
        if ($nickname = $searchData['nickname'] ?? '') {
            $model->where('nickname', 'like', "%{$nickname}%");
        }
        if ($no = $searchData['no'] ?? '') {
            $model->where('no', 'like', "%{$no}%");
        }
        $is_pay = $searchData['is_pay'] ?? -1;
        if ($is_pay >= 0) {
            $model->where('is_pay', $is_pay);
        }
        $status = $searchData['status'] ?? -1;
        if ($status >= 0) {
            $model->where('status', $status);
        }
        $type = $searchData['type'] ?? -1;
        if ($type >= 1) {
            $model->where('type', $type);
        }
        if ($created_at = $searchData['created_at'] ?? []) {
            $model->whereBetween('created_at', $created_at);
        }

        $result = $model->paginate(config('app.limit'));

        return $this->writeJson(0, [
            'data' => $result->items(),
            'count' => $result->total()
        ]);
    }
}
